/*
 * SetStateSystemPriceSchemeForm.java
 *
 * Created on November 20, 2008, 11:21 AM
 */

package rrs;
import java.sql.*;
/**
 *
 * @author  Paul Molenda
 */
public class SetStateSystemPriceSchemeForm extends javax.swing.JFrame {

    /** Creates new form SetStateSystemPriceSchemeForm */
    public SetStateSystemPriceSchemeForm() {
        initComponents();
        conn = DBInterface.dbConnOpen();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblSystem = new javax.swing.JLabel();
        btnActivate = new javax.swing.JButton();
        btnDeactivate = new javax.swing.JButton();
        txtEnterSystemID = new javax.swing.JTextField();
        lblmisc = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblSystem.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblSystem.setText("Enter System Price Scheme ID");
        lblSystem.setName("lblSystem"); // NOI18N

        btnActivate.setText("Activate");
        btnActivate.setName("btnActivate"); // NOI18N
        btnActivate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnActivateMouseClicked(evt);
            }
        });

        btnDeactivate.setText("Deactivate");
        btnDeactivate.setName("btnDeactivate"); // NOI18N
        btnDeactivate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnDeactivateMouseClicked(evt);
            }
        });

        txtEnterSystemID.setName("txtEnterSystemID"); // NOI18N

        lblmisc.setName("lblmisc"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addComponent(txtEnterSystemID, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(btnActivate, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(44, 44, 44)
                        .addComponent(btnDeactivate)))
                .addContainerGap(70, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(54, Short.MAX_VALUE)
                .addComponent(lblSystem, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblmisc)
                .addContainerGap(339, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addComponent(lblSystem, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtEnterSystemID, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnActivate, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDeactivate, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addComponent(lblmisc)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
/*
 * The user has clicked the activate button
 * Activate the Price Scheme only if it is
 * deactive.
 */ 
private void btnActivateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnActivateMouseClicked
  if (btnActivate.isEnabled() == true)
  {     
    String rtn = "";
    String id = txtEnterSystemID.getText();
    try
     {
        if (determineState(id))
        {
            lblmisc.setText("The id: " + id + " is already active.");            
        }
        else
        {
            if (setState(id,true))
            {
                lblmisc.setText("The id: " + id + " is now set to active.");
                btnActivate.setEnabled(false);
                btnDeactivate.setEnabled(false);
            }		 
        }
     } catch (SQLException e) {
          //They entered something wrong
			rtn = e.getMessage();
         txtEnterSystemID.setText("");
         lblmisc.setText("The ID entered is not a valid System Price Scheme.");
     }
  } 
}//GEN-LAST:event_btnActivateMouseClicked
/*
 * This method determines the State of the Price Scheme
 * It returns true if the Scheme is active
 * False if it is deactive
 */ 
private boolean determineState(String id)throws SQLException
{
         boolean state = false;
         String rtn = "";
         Statement stmt = conn.createStatement();
         ResultSet set = stmt.executeQuery("SELECT FROM SYSTEMPRICESCHEME WHERE SYSTEMPRICESCHEMEID = '" + id + "'"); 
         if (set.getBoolean(5))
         {
             state =  true;
         }
         else
         {
             state = false;
         }
         stmt.close();
         return state;
}
/*
 * This method sets the state of the Price Scheme
 */ 
private boolean setState(String id, boolean state)
{
    boolean setOk = false;
    try {
			Statement stmt = conn.createStatement();
			stmt.executeQuery("UPDATE SYSTEMPRICESCHEME SET ISACTIVE = " + state + " where SYSTEMPRICESCHEMEID = '" + id + "'");					 
			stmt.close();
			setOk = true;
        } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			setOk = false;
        }		
    return setOk;
}
private void btnDeactivateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDeactivateMouseClicked
if (btnDeactivate.isEnabled() == true)
{
    String rtn = "";
    String id = txtEnterSystemID.getText();
    try
     {
        if (determineState(id))
        {
            if (setState(id,false))
            {
              lblmisc.setText("The id: " + id + " is now set to deactive.");
              btnActivate.setEnabled(false);
              btnDeactivate.setEnabled(false);
            }          
        }
        else
        {            
              lblmisc.setText("The id: " + id + " is already deactive.");              		 
        }
     }
    catch (SQLException e) 
     {
          //They entered something wrong
			rtn = e.getMessage();
         txtEnterSystemID.setText("");
         lblmisc.setText("The ID entered is not a valid System Price Scheme.");
     }
 }
}//GEN-LAST:event_btnDeactivateMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActivate;
    private javax.swing.JButton btnDeactivate;
    private javax.swing.JLabel lblSystem;
    private javax.swing.JLabel lblmisc;
    private javax.swing.JTextField txtEnterSystemID;
    // End of variables declaration//GEN-END:variables
    Connection conn;
}
